package io.leo.leoutils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Set;

public class Property extends HashMap<String, Property.GetterAndSetter> {
    class GetterAndSetter{
        private Method getter;
        private Method setter;

        public GetterAndSetter() {
            super();
        }

        public GetterAndSetter(Method getter, Method setter) {
            this.getter = getter;
            this.setter = setter;
        }

        public Method getGetter() {
            return getter;
        }

        public void setGetter(Method getter) {
            this.getter = getter;
        }

        public Method getSetter() {
            return setter;
        }

        public void setSetter(Method setter) {
            this.setter = setter;
        }
    }


    public Property() {
        super();
    }

    public Property(String name, Method getter, Method setter) {
        put(name,new GetterAndSetter(getter,setter));
    }

    public void putGetterAuto(String name,Method getter){
        if (get(name)!=null)
            put(name,new GetterAndSetter(getter,get(name).setter));
        else putGetterAndSetter(name,getter,null);
    }

    public void putSetterAuto(String name,Method setter){
        if (get(name)!=null)
            put(name,new GetterAndSetter(get(name).getter,setter));
        else putGetterAndSetter(name,null,setter);
    }

    private void putGetterAndSetter(String name, Method getter, Method setter) {
        put(name,new GetterAndSetter(getter,setter));
    }

    public Method getSetter(String name){
        return get(name).setter;
    }

    public Method getGetter(String name){
        return get(name).getter;
    }

    public void read(ReflectUtils.PropertyReadCallback callback){
        Set<String> keySet = keySet();
        for (String key:keySet){
            callback.onRead(key,get(key).getter,get(key).setter);
        }
    }
}
